<?php
/**
 * --------------------------------------------------------------------
 * MCore精简框架
 * 作者：阿枫，QQ：52037872，Email：support@mdeve.com
 * --------------------------------------------------------------------
 *
 * SVN revision information:
 * @version $Revision: 1 $:
 * @author  $Author: zy $:
 * @date      $Date: 2017-03-10 21:12:21 +0800 (星期五, 10 三月 2017) $:
 */

class _ModuleClass extends __basic_def {


    function __construct() {
        global $_M;
        $module_import_file = DATA_PATH. "/runtime/module.php";
        if(file_exists($module_import_file)) {
            $_M->module_installed = require $module_import_file;
        } else {
            $_M->db();
            $row = $_M->db->get_row(array("table"=>"options", "field"=>"options_value", "where"=>"type='system' and options_name='module_installed' and category='hide'"));
            if(is_array($row)) {
                $arr = json_decode($row["options_value"], true);
                $php = "<?php\nreturn array(\"" . implode("\",\"", $arr) . "\");\n?>";
                if(!is_writable(dirname($module_import_file))) exit("缓存目录权限错误，请确保 " . DATA_PATH . " 目录有写入权限！");
                file_put_contents($module_import_file, $php);
                $_M->module_installed = json_decode($row["options_value"], true);
            }
        }

        // 初始化所有已安装的插件
        if(sizeof($_M->module_installed)) {
            foreach($_M->module_installed as $module) {
                $module_file = MODULE_PATH. DIRECTORY_SEPARATOR . $module . DIRECTORY_SEPARATOR . "module.php";
                if(file_exists($module_file)) {
                    require $module_file;
                    $module_class_name = "_". ucfirst($module) . "ModuleClass";
                    $interface = new ReflectionClass("_module_interface");
                    if(class_exists($module_class_name)) {
                        $module_class = new ReflectionClass($module_class_name);
                        if ($module_class->isSubclassOf($interface)) {
                            $module = $module_class->newInstance();
                            $method = $module_class->getMethod("initialization");
                            $method->invoke($module);
                        }
                    }
                }
            }
        }
    }


    /**
     * 返回模块简介
     * @param $module_name string
     * @return string
     */
    function get_intro($module_name) {
        return $this->get_info($module_name, "intro");
    }

    function get_version($module_name) {
        return $this->get_info($module_name, "version");
    }

    private function get_info($module_name, $field){
        $result = "";
        $module_class_name = "_". ucfirst($module_name) . "ModuleClass";
        $interface = new ReflectionClass("_module_interface");

        $module_file = MODULE_PATH. DIRECTORY_SEPARATOR . $module_name . DIRECTORY_SEPARATOR . "module.php";
        require_once $module_file;

        $module_class = new ReflectionClass($module_class_name);
        if ($module_class->isSubclassOf($interface)) {
            $module = $module_class->newInstance();
            $method = $module_class->getMethod($field);
            $result = $method->invoke($module);
        }

        return $result ?? "获取出错";
    }

}